package com.ks.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.util.ToolUtil;
import com.ks.user.entity.PowerRole;
import com.ks.user.entity.PowerUserRole;
import com.ks.user.dao.PowerUserRoleMapper;
import com.ks.user.service.IPowerRoleService;
import com.ks.user.service.IPowerUserRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.user.service._ServiceBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-11-30
 */
@Service
public class PowerUserRoleServiceImpl extends _ServiceBase<PowerUserRoleMapper, PowerUserRole> implements IPowerUserRoleService {
    @Resource
    private PowerUserRoleMapper roleMapper;

    @Resource
    private IPowerRoleService powerRoleService;


    @Override
    public int addRoleList(OperationContext context, List<Integer> ids, Integer userId) {
        int count = 0;
        PowerUserRole userRole = new PowerUserRole();
        if (ToolUtil.isNotEmpty(ids)) {
            for (int i = 0; i < ids.size(); i++) {
                userRole.setIRoleID(ids.get(i));
                userRole.setIUserID(userId);
                userRole.setCreateUserID(context.getUserId().intValue());
                userRole.setTenantID(context.getTenantId().intValue());
                userRole.setOrgID(context.getOrgId().intValue());
                userRole.setOrgSubID(context.getOrgSubId().intValue());
                userRole.setDataType("TENANT");
                PowerUserRole role = roleMapper.selectOne(userRole);
                if (ToolUtil.isNotEmpty(role)) {
                    role.setDelMark(Const.DEL_FLAG_NORMAL);
                    Integer integer = roleMapper.updateById(role);
                    count += integer;
                } else {
                    Integer insert = roleMapper.insert(userRole);
                    count += insert;
                }
            }
        }
        return count;
    }

    @Override
    public int moveRoleList(OperationContext context, List<Integer> ids, Integer userId) {
        int count = 0;
        PowerUserRole userRole = new PowerUserRole();
        if (ToolUtil.isNotEmpty(ids)) {
            for (int i = 0; i < ids.size(); i++) {
                userRole.setIUserID(userId);
                userRole.setIRoleID(ids.get(i));
                userRole.setTenantID(context.getTenantId().intValue());
                PowerUserRole powerUserRole = roleMapper.selectOne(userRole);
                if (ToolUtil.isNotEmpty(powerUserRole)) {
                    powerUserRole.setDelMark(Const.DEL_FLAG_DEL);
                    Integer integer = roleMapper.updateById(powerUserRole);
                    count += integer;
                }
            }
        }
        return count;
    }

    @Override
    public Map<String, Object> getUserRoleList(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        Map<String, Object> map = new HashMap<>();
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<PowerUserRole> wrapper = new EntityWrapper<>();
        PowerUserRole userRole = new PowerUserRole();
        userRole.setTenantID(context.getTenantId().intValue());
        userRole.setDelMark(Const.DEL_FLAG_NORMAL);
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("iUserID".equals(field.getName())) {
                    userRole.setIUserID(Integer.valueOf(field.getValue()));
                }
            }
        }
        wrapper.setEntity(userRole);
        Integer integer = roleMapper.selectCount(wrapper);
        map.put("count", integer);
        List<PowerUserRole> powerUserRoles = roleMapper.selectPage(new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit()), wrapper);
        List<PowerRole> list = new ArrayList<>();
        if (ToolUtil.isNotEmpty(powerUserRoles)) {
            for (PowerUserRole powerUserRole : powerUserRoles) {
                PowerRole powerRole = powerRoleService.selectById(powerUserRole.getIRoleID());
                if (ToolUtil.isNotEmpty(powerRole)) {
                    list.add(powerRole);
                }
            }

        }
        map.put("list", list);
        return map;
    }
}
